<template>
  <div class="reset-password-container">
    <el-card class="reset-card">
      <div class="reset-header">
        <h2>重置密码</h2>
        <p>请输入您的重置令牌进行密码重置</p>
      </div>
      
      <el-form
        ref="resetForm"
        :model="resetFormData"
        :rules="rules"
        label-position="top"
        @submit.prevent="handleReset"
      >
        <el-form-item label="用户名" prop="username">
          <el-input 
            v-model="resetFormData.username"
            placeholder="请输入用户名"
          />
        </el-form-item>
        
        <el-form-item label="重置令牌" prop="resetToken">
          <el-input 
            v-model="resetFormData.resetToken"
            placeholder="请输入重置令牌"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            class="reset-button" 
            :loading="loading" 
            @click="handleReset"
          >
            重置密码
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-link">
        已有账号? 
        <router-link to="/login">立即登录</router-link>
      </div>
      
      <el-alert
        v-if="resetSuccess"
        title="密码重置成功！"
        type="success"
        description="您的密码已重置为123456789，请立即登录并修改密码。"
        show-icon
        :closable="false"
        class="reset-alert"
      />
    </el-card>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'

export default {
  name: 'ResetPasswordPage',
  setup() {
    const router = useRouter()
    const resetFormRef = ref(null)
    const loading = ref(false)
    const resetSuccess = ref(false)
    
    const formData = reactive({
      username: '',
      resetToken: ''
    })
    
    const rules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      resetToken: [
        { required: true, message: '请输入重置令牌', trigger: 'blur' }
      ]
    }
    
    const handleReset = async () => {
      if (!resetFormRef.value) return
      
      try {
        await resetFormRef.value.validate(async (valid) => {
          if (valid) {
            loading.value = true
            
            try {
              const response = await axios.post('/api/password/reset', formData)
              
              if (response.data.success) {
                resetSuccess.value = true
                ElMessage.success('密码重置成功，新密码为：123456789')
                
                // 3秒后跳转到登录页
                setTimeout(() => {
                  router.push('/login')
                }, 3000)
              } else {
                ElMessage.error(response.data.message || '重置失败')
              }
            } catch (error) {
              ElMessage.error(error.response?.data?.message || '重置失败，请稍后重试')
            } finally {
              loading.value = false
            }
          }
        })
      } catch (error) {
        ElMessage.error('操作异常，请稍后重试')
      }
    }
    
    return {
      resetForm: resetFormRef,
      resetFormData: formData,
      rules,
      loading,
      resetSuccess,
      handleReset
    }
  }
}
</script>

<style scoped>
.reset-password-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: calc(100vh - 140px);
  background-color: #f5f7fa;
}

.reset-card {
  width: 400px;
  padding: 20px;
}

.reset-header {
  text-align: center;
  margin-bottom: 30px;
}

.reset-header h2 {
  font-size: 24px;
  color: #303133;
  margin-bottom: 10px;
}

.reset-header p {
  font-size: 14px;
  color: #909399;
}

.reset-button {
  width: 100%;
}

.login-link {
  margin-top: 20px;
  text-align: center;
  font-size: 14px;
  color: #606266;
}

.login-link a {
  color: #409EFF;
  text-decoration: none;
}

.reset-alert {
  margin-top: 20px;
}
</style> 